package one;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * @author wangpp
 */
public class GsonSample {
    public static void main(String[] args) {
//        createGson();
//        createGsonByBuilder();
//        jsonToObj();
//        objToJson();
//        prettyPrint();
//        excludeFields();
//        excludeStrategies();
//        serializingNull();
//        customInstanceCreator();
        versionInGson();
    }

    private static void versionInGson() {
        Gson gson = new GsonBuilder().setVersion(2.0).create();

        Person person = new Person();
        person.firstName = "John";
        person.lastName = "Doe";
        person.middleName = "Blocks";
        person.email = "john@doe.com";
        String personJson = gson.toJson(person);
        //{"firstName":"John","lastName":"Doe","middleName":"Blocks"}
        System.out.println(personJson);

//
        String personJson2 = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"middleName\":\"Blocks\",\"email\":\"john@doe.com\"}";
        Person personRead = gson.fromJson(personJson2, Person.class);
        //Person{firstName='John', lastName='Doe', middleName='Blocks', email='null'}
        System.out.println(personRead);
    }

    private static void customInstanceCreator() {
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(Car.class, new CarCreator())
                .create();
        String carJson = "{ \"doors\" : 4 }";

        Car car = gson.fromJson(carJson, Car.class);

        //Car{brand='xxx', doors=4}
        System.out.println(car);

    }

    private static void serializingNull() {
        Gson gson = new GsonBuilder()
                .serializeNulls()
                .create();

        Car xxx = new Car(null, 111);

        //{"brand":null,"doors":111}
        System.out.println(gson.toJson(xxx));

    }

    private static void excludeStrategies() {
        ExclusionStrategy exclusionStrategy = new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes fieldAttributes) {
                if ("brand".equals(fieldAttributes.getName())) {
                    return true;
                }
                return false;
            }

            @Override
            public boolean shouldSkipClass(Class aClass) {
                return false;
            }
        };


        Gson gson = new GsonBuilder().setExclusionStrategies(exclusionStrategy).create();

        String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
        Car car = gson.fromJson(json, Car.class);
        System.out.println(car);

        Car xxx = new Car("xxx", 111);
        System.out.println(gson.toJson(xxx));
    }

    private static void excludeFields() {
        Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();


        String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
        Car car = gson.fromJson(json, Car.class);
        System.out.println(car);

        Car xxx = new Car("xxx", 111);
        System.out.println(gson.toJson(xxx));
    }


    private static void prettyPrint() {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Car xxx = new Car("xxx", 111);
        //{
        //  "brand": "xxx",
        //  "doors": 111
        //}
        System.out.println(gson.toJson(xxx));
    }

    private static void objToJson() {
        Gson gson = new Gson();
        Car xxx = new Car("xxx", 111);
        String s = gson.toJson(xxx);
        System.out.println(s);
    }

    private static void jsonToObj() {
        Gson gson = new Gson();
        String json = "{\"brand\":\"Jeep\", \"doors\": 3}";
        Car car = gson.fromJson(json, Car.class);
        System.out.println(car);

    }

    private static void createGsonByBuilder() {
        //1 create builder
        GsonBuilder builder = new GsonBuilder();

        //2 config builder
//        builder.addDeserializationExclusionStrategy(xx);
//        builder.addSerializationExclusionStrategy(xxx)

        //3 create gson
        Gson gson = builder.create();
    }

    private static void createGson() {
        Gson gson = new Gson();
    }
}
